package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;

import com.itheima.health.pojo.entity.Number;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {
        @Autowired
        private ReportService reportService;
        /**
         * 套餐预约占⽐
         */
        @GetMapping("/getSetmealReport")
        public Result getSetmealReport() {
            List<Map<String, Objects>> setmealCount = reportService.countBySetmeal();
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Objects> map : setmealCount) {
                setmealNames.add(String.valueOf(map.get("name")));
            }

            //构造返回值
            Map map = new HashMap();
            map.put("setmealCount", setmealCount);
            map.put("setmealNames", setmealNames);
            return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        }

        /**
         * 查询用户报告
         *
         * @return
         */
        @RequestMapping("/getMemberReport")
        public Result getMemberReport() {

            List<String> months = new ArrayList<>();
            List<Integer> memeberCount = null;
            //构造months
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -12);
            DateFormat df = new SimpleDateFormat("yyyy.MM");
            for (int i = 0; i < 12; i++) {
                months.add(df.format(cal.getTime()));
                cal.add(Calendar.MONTH, 1);
            }

            //构造memberCount
            memeberCount = reportService.countByMonth(months);

            Map<String, Object> map = new HashMap<>();
            map.put("months", months);
            map.put("memberCount", memeberCount);
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        }

        @GetMapping("/getBusinessReportData")
        public Result businessData() {
            //获得当天的开始时间
            LocalDateTime beginToday = LocalDateTime.now().with(LocalTime.MIN);
            //获得当天结束时间
            LocalDateTime endToday = LocalDateTime.now().with(LocalTime.MAX);
            //获取本周的开始时间
            LocalDate beginWeek = LocalDate.now().minusDays(7);
            //获取本周的结束时间
            LocalDate endWeek = LocalDate.now().minusDays(1);
            //获取本月的开始时间
            LocalDate beginMonth = LocalDate.now().minusDays(30);
            //获取本月的结束时间
            LocalDate endMonth = LocalDate.now().minusDays(1);

            Number number = reportService.getBusinessData(beginToday, endToday, beginWeek, endWeek, beginMonth, endMonth);

            return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, number);
        }

        @GetMapping("/exportBusinessReport")
        public void exportBusinessReport(HttpServletResponse response) {
            log.info("报表导出...");
            reportService.export(response);
        }
    }

